
package com.gs.netty.support.handler.adapter;


import com.gs.netty.core.handler.adapter.HandlerAdapter;

import com.gs.netty.core.resolver.HandlerMethodReturnValueHandler;

import com.gs.netty.core.resolver.args.HandlerMethodArgumentResolver;
import com.gs.netty.support.resolver.ExceptionHandlerMethodReturnValueHandler;
import com.gs.netty.support.resolver.JsonHandlerMethodReturnValueHandler;
import com.gs.netty.support.resolver.RequestBodyJsonMethodProcessor;
import com.gs.netty.support.resolver.RequestResponseMethodArgumentResolver;
import org.springframework.beans.factory.InitializingBean;

import java.util.ArrayList;
import java.util.List;

public abstract class AbstractHandlerAdapter<Request,Response,Handler> implements HandlerAdapter<Request,Response,Handler>, InitializingBean {

    /**
     * 参数解析器
     */
    protected HandlerMethodArgumentResolver argumentResolvers;


    /**
     * 结果处理器
     */
    protected HandlerMethodReturnValueHandler returnValueHandlers;

    // 获取默认参数解析器
    protected List<HandlerMethodArgumentResolver> getDefaultArgumentResolvers(){
        List<HandlerMethodArgumentResolver> resolvers = new ArrayList<>();
        // 添加默认参数解析器
        resolvers.add(new RequestResponseMethodArgumentResolver());
        resolvers.add(new RequestBodyJsonMethodProcessor());
        return resolvers;
    }

    // 获取默认结果处理器
    protected List<HandlerMethodReturnValueHandler> getDefaultReturnValueHandlers(){
        List<HandlerMethodReturnValueHandler> returnValueHandlers = new ArrayList<>();
        // 添加默认参数解析器
        returnValueHandlers.add(new JsonHandlerMethodReturnValueHandler());
        returnValueHandlers.add(new ExceptionHandlerMethodReturnValueHandler());
        return returnValueHandlers;
    }


    protected  abstract void initArgumentResolvers();
    protected  abstract void initReturnValueHandlers();


    @Override
    public void afterPropertiesSet() throws Exception {
        initArgumentResolvers();
        initReturnValueHandlers();
    }
}
